package cn.ligoo.dtu.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.ligoo.dtu.Constants;
import cn.ligoo.dtu.dao.BmuSlaveBalanceCurrentDao;
import cn.ligoo.dtu.dao.BmuSlaveBatteryStateDao;
import cn.ligoo.dtu.dao.BmuSlaveBoxTempDao;
import cn.ligoo.dtu.dao.BmuSlaveInfoDao;
import cn.ligoo.dtu.dao.DtuInfoDao;
import cn.ligoo.dtu.domain.BmuSlaveBalanceCurrent;
import cn.ligoo.dtu.domain.BmuSlaveBatteryState;
import cn.ligoo.dtu.domain.BmuSlaveBoxTemp;
import cn.ligoo.dtu.domain.BmuSlaveInfo;
import cn.ligoo.dtu.domain.DtuInfo;
import cn.ligoo.dtu.service.BmuSlaveInfoService;

/**
 * @author Jin,QingHua
 * @version 2012-07-24 12:18:52
 */
@Service
public class BmuSlaveInfoServiceImpl implements BmuSlaveInfoService {

	@Resource
	private DtuInfoDao dtuInfoDao;

	@Resource
	private BmuSlaveInfoDao bmuSlaveInfoDao;

	@Resource
	private BmuSlaveBalanceCurrentDao bmuSlaveBalanceCurrentDao;

	@Resource
	private BmuSlaveBatteryStateDao bmuSlaveBatteryStateDao;

	@Resource
	private BmuSlaveBoxTempDao bmuSlaveBoxTempDao;

	@Override
	public Integer createBmuSlaveInfo(BmuSlaveInfo t) {
		DtuInfo di = new DtuInfo();
		di.setId(t.getDtu_info_id());
		di.setIs_online(Constants.BYTE_1);
		di.setRefresh_time(new Date());
		this.dtuInfoDao.updateEntity(di);

		this.bmuSlaveInfoDao.insertEntity(t);
		Integer bmu_slave_id = t.getId();

		List<BmuSlaveBalanceCurrent> currentList = t.getCurrentList();
		for (BmuSlaveBalanceCurrent current : currentList) {
			current.setBmu_slave_id(bmu_slave_id);
			this.bmuSlaveBalanceCurrentDao.insertEntity(current);
		}

		List<BmuSlaveBatteryState> stateList = t.getStateList();
		for (BmuSlaveBatteryState state : stateList) {
			state.setBmu_slave_id(bmu_slave_id);
			this.bmuSlaveBatteryStateDao.insertEntity(state);
		}

		List<BmuSlaveBoxTemp> tempList = t.getTempList();
		for (BmuSlaveBoxTemp temp : tempList) {
			temp.setBmu_slave_id(bmu_slave_id);
			this.bmuSlaveBoxTempDao.insertEntity(temp);
		}

		return bmu_slave_id;
	}

	@Override
	public int modifyBmuSlaveInfo(BmuSlaveInfo t) {
		return this.bmuSlaveInfoDao.updateEntity(t);
	}

	@Override
	public int removeBmuSlaveInfo(BmuSlaveInfo t) {
		return this.bmuSlaveInfoDao.deleteEntity(t);
	}

	@Override
	public BmuSlaveInfo getBmuSlaveInfo(BmuSlaveInfo t) {
		return this.bmuSlaveInfoDao.selectEntity(t);
	}

	@Override
	public Integer getBmuSlaveInfoCount(BmuSlaveInfo t) {
		return this.bmuSlaveInfoDao.selectEntityCount(t);
	}

	@Override
	public List<BmuSlaveInfo> getBmuSlaveInfoList(BmuSlaveInfo t) {
		return this.bmuSlaveInfoDao.selectEntityList(t);
	}

}
